import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { EditUserGroupService } from './edit-user-group.service';
import { Response} from '@angular/http';
import {GlobalService} from "../../../global.service";

@Component({
  selector: 'app-edit-user-group',
  templateUrl: './edit-user-group.component.html',
  styleUrls: ['./edit-user-group.component.css']
})
export class EditUserGroupComponent implements OnInit {

  code:string;
  addProblemSetCode:string;
  data:UserGroupsInfo = new UserGroupsInfo;
  problemSetInfos:ProblemSetInfo[] = [];
  addProblemSetFlag:boolean=false;
  addedProblemSetInfos:ProblemSetInfo[] = [];
  addedProblemSetCodes:string='';
  addedPinCodeInfos:PinCodeInfo[] = [];
  addUserText:string;
  addedUserInfo:UserInfo[] = [];


  constructor(
    private location:Location,
    private globalService:GlobalService,
    private editUserGroupService:EditUserGroupService
  ) {}

  ngOnInit() {
    let url = this.location.path();
    this.code = url.substring(url.indexOf("userGroups/")+11,url.lastIndexOf("/"));
    this.editUserGroupService.findUserGroupInfo({code:this.code}).subscribe((res:Response)=>{
      this.data.code = res.json().value.code;
      this.data.name = res.json().value.name;
      this.data.remark = res.json().value.remark;
      this.data.checkCode = res.json().value.checkCode;

      if(res.json().value.problemSetCodes){
        let codes = res.json().value.problemSetCodes.split(",");
        for(let i=0;i<codes.length;i++){
          this.addedProblemSetInfos.push({
            code:codes[i].substring(0,codes[i].indexOf("-")),
            name:codes[i].substring(codes[i].indexOf("-")+1)
          });
          this.addedProblemSetCodes+=codes[i].substring(0,codes[i].indexOf("-"))+",";
        }
        this.addedProblemSetCodes = this.addedProblemSetCodes.substring(0,this.addedProblemSetCodes.length-1);
      }
      this.addedPinCodeInfos = [];
      if(res.json().value.pinCodes){
        let codes = res.json().value.pinCodes.split(",");
        for(let i=0;i<codes.length;i++){
          this.addedPinCodeInfos.push({
            code:codes[i].substring(0,codes[i].indexOf("_")),
            time:codes[i].substring(codes[i].indexOf("_")+1)
          });
        }
      }
      this.addedUserInfo = [];
      if(res.json().value.users){
        for(let i=0;i<res.json().value.users.length;i++){
          this.addedUserInfo.push({
            code:res.json().value.users[i].code,
            studentNumber:res.json().value.users[i].studentNumber,
            name:res.json().value.users[i].name,
            email:res.json().value.users[i].email,
            checkCode:null,
          });
        }
      }
    });
    this.editUserGroupService.findProblemSetList({properties:{creator:this.globalService.getCurrentUser().code}}).subscribe((res:Response)=>{
      console.log("problemSetInfos:",res.json());

      for(let i=0;i<res.json().rows.length;i++){
        var item = res.json().rows[i];
        this.problemSetInfos.push({
          code:item.code,
          name:item.name
        });
      }

      if(this.problemSetInfos.length>0){
        this.addProblemSetCode = this.problemSetInfos[0].code;
      }
    });
  }

  update(){
    this.data.properties.update = true;
    this.editUserGroupService.saveUserGroupInfo(this.data).subscribe((res:Response)=>{
      this.data.name = res.json().value.name;
      this.data.remark = res.json().value.remark;
    });
  }

  showAddProblemSet(){

    this.editUserGroupService.findProblemSetList({properties:{creator:this.globalService.getCurrentUser().code,code_notin:this.addedProblemSetCodes}}).subscribe((res:Response)=>{
      console.log("problemSetInfos:",res.json());
      this.problemSetInfos = [];

      for(let i=0;i<res.json().rows.length;i++){
        var item = res.json().rows[i];
        this.problemSetInfos.push({
          code:item.code,
          name:item.name
        });
      }
      if(this.problemSetInfos.length>0){
        this.addProblemSetCode = this.problemSetInfos[0].code;
        this.addProblemSetFlag = true;
      }else{
        alert("当前没有可选题目集");
      }
    });
  }

  addProblemSet(){
    console.log(this.addProblemSetCode);

    this.editUserGroupService.addProblemSetToUserGroup({
      code:this.data.code,
      problemSetCodes:this.addProblemSetCode
    }).subscribe((res:Response)=>{
      this.addedProblemSetInfos = [];
      this.addedProblemSetCodes = '';
      let codes = res.json().value.problemSetCodes.split(",");
      for(let i=0;i<codes.length;i++){
        this.addedProblemSetInfos.push({
          code:codes[i].substring(0,codes[i].indexOf("-")),
          name:codes[i].substring(codes[i].indexOf("-")+1)
        });
        this.addedProblemSetCodes+=codes[i].substring(0,codes[i].indexOf("-"))+",";
      }
      this.addedProblemSetCodes = this.addedProblemSetCodes.substring(0,this.addedProblemSetCodes.length-1);
      this.addProblemSetFlag = false;
    });
  }

  deleteProblemSet(event){
    this.editUserGroupService.deleteProblemSetFromUserGroup({
      code:this.data.code,
      problemSetCodes:event.target.name
    }).subscribe((res:Response)=>{
      this.addedProblemSetInfos = [];
      this.addedProblemSetCodes = '';
      if(res.json().value.problemSetCodes&&res.json().value.problemSetCodes.length>0){
        let codes = res.json().value.problemSetCodes.split(",");
        for(let i=0;i<codes.length;i++){
          this.addedProblemSetInfos.push({
            code:codes[i].substring(0,codes[i].indexOf("-")),
            name:codes[i].substring(codes[i].indexOf("-")+1)
          });
          this.addedProblemSetCodes+=codes[i].substring(0,codes[i].indexOf("-"))+",";
        }
        this.addedProblemSetCodes = this.addedProblemSetCodes.substring(0,this.addedProblemSetCodes.length-1);
      }
    });
  }

  addPinCode(){
    this.editUserGroupService.addPinCodeToUserGroup({
      code:this.data.code,
    }).subscribe((res:Response)=>{
      this.addedPinCodeInfos = [];

      if(res.json().value.pinCodes){
        let codes = res.json().value.pinCodes.split(",");
        for(let i=0;i<codes.length;i++){
          this.addedPinCodeInfos.push({
            code:codes[i].substring(0,codes[i].indexOf("_")),
            time:codes[i].substring(codes[i].indexOf("_")+1)
          });
        }
      }
    });
  }

  deletePinCode(event){
    console.log(event.target.name);
    this.editUserGroupService.deletePinCodeFromUserGroup({
      code:this.data.code,
      pinCodes:event.target.name
    }).subscribe((res:Response)=>{
      this.addedPinCodeInfos = [];
      if(res.json().value.pinCodes){
        let codes = res.json().value.pinCodes.split(",");
        for(let i=0;i<codes.length;i++){
          this.addedPinCodeInfos.push({
            code:codes[i].substring(0,codes[i].indexOf("_")),
            time:codes[i].substring(codes[i].indexOf("_")+1)
          });
        }
      }

    });
  }

  addUser(){
    let userInfos:UserInfo[] = [];
    let users = this.addUserText.split("\n");
    for(let i=0;i<users.length;i++){
      userInfos.push({
        code:'',
        email:'',
        studentNumber:users[i].split(" ")[0],
        name:users[i].split(" ")[1],
        checkCode:this.data.checkCode
      });
    }
    this.editUserGroupService.addAccountInfoToUserGroup({
      code:this.data.code,
      users:userInfos
    }).subscribe((res:Response)=>{
      this.addedUserInfo = [];
      if(res.json().value.users){
        for(let i=0;i<res.json().value.users.length;i++){
          this.addedUserInfo.push({
            code:res.json().value.users[i].code,
            studentNumber:res.json().value.users[i].studentNumber,
            name:res.json().value.users[i].name,
            email:res.json().value.users[i].email,
            checkCode:null
          });
        }
      }
    });
  }

  deleteUser(event){
    console.log(event.target.name);
    this.editUserGroupService.deleteUserFromUserGroup({
      code:this.data.code,
      users:[{
        code:event.target.name
      }]
    }).subscribe((res:Response)=>{
      this.addedUserInfo = [];
      if(res.json().value.users){
        for(let i=0;i<res.json().value.users.length;i++){
          this.addedUserInfo.push({
            code:res.json().value.users[i].code,
            studentNumber:res.json().value.users[i].studentNumber,
            name:res.json().value.users[i].name,
            email:res.json().value.users[i].email,
            checkCode:null
          });
        }
      }
    })
  }
}

class ProblemSetInfo{
  code:string;
  name:string;
}
class PinCodeInfo{
  code:string;
  time:string;
}
class UserInfo{
  code:string;
  studentNumber:string;
  name:string;
  email:string;
  checkCode:string;
}
class UserGroupsInfo{
  code:string;
  name:string;
  remark:string;
  checkCode:string;
  properties:Properties=new Properties;
}
class Properties {
  update:any;
}

